package com.zsl.swing.redis.desktop.window.help;

import com.zsl.swing.redis.desktop.model.CommandModel;
import com.zsl.swing.redis.desktop.utils.CmdPromptModelUtils;
import com.zsl.swing.redis.desktop.utils.CollectionUtils;

import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import java.util.List;
import java.util.Objects;

public class ZslTableModel extends AbstractTableModel {
    private static final String[] COLUMN_NAMES = new String[]{"语法", "描述", "版本","时间复杂度"};


    private List<CommandModel> dataList;

    public ZslTableModel() {
        this.initData(null, null);

//        System.out.println("当前展示命令：" + JSON.toJSONString(this.dataList));
    }

    private void initData(String cmdName, String group) {
        this.dataList = CmdPromptModelUtils.queryLeftLike(cmdName, group);
    }

    @Override
    public int getRowCount() {
        return dataList.size();
    }

    @Override
    public int getColumnCount() {
        return COLUMN_NAMES.length;
    }

    @Override
    public String getColumnName(int columnIndex) {
        return COLUMN_NAMES[columnIndex];
    }

    @Override
    public Class<CommandModel> getColumnClass(int columnIndex) {
        return CommandModel.class;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if(CollectionUtils.isEmpty(this.dataList) || rowIndex >= this.dataList.size()){
            return null;
        }

        CommandModel commandModel = this.dataList.get(rowIndex);
        switch (columnIndex){
            case 0:
                return commandModel.getSyntax().replaceAll("\\s+", " ");
//            case 1:
//                return commandModel.getArguments();
            case 1:
                return commandModel.getSummary();
            case 2:
                return commandModel.getSince();
            case 3:
                return this.cutTimeComplexity(commandModel.getTimeComplexity());

                default:return null;
        }
    }

    private String cutTimeComplexity(String origin){
        StringBuilder sb = new StringBuilder();

        if(!Objects.isNull(origin)){
            int num = 0,index = 0;
            for(int i = 0; i< origin.length(); i++){
                char c = origin.charAt(i);
                sb.append(c);

                if('(' == c){
                    num++;
                    index++;
                }

                if(')' == c){
                    index--;
                }

                if(num > 0 && index == 0){
                    break;
                }

            }
        }

        return sb.toString();
    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if(CollectionUtils.isEmpty(this.dataList) || rowIndex >= this.dataList.size()){
            return;
        }

        CommandModel commandModel = this.dataList.get(rowIndex);
        String cellValue = String.valueOf(aValue);
        switch (columnIndex){
            case 0:
                commandModel.setSyntax(cellValue);
//                int index = cellValue.indexOf(" ");
//                commandModel.setCmd(cellValue.substring(0, index + 1));
//
//                if(cellValue.length() > index + 1){
//                    commandModel.setArguments(cellValue.substring(index + 2));
//                }
                break;
//            case 1:
//                commandModel.setArguments(cellValue);
//                break;
            case 1:
                commandModel.setSummary(cellValue);
                break;
            case 2:
                commandModel.setSince(cellValue);
                break;
            case 3:
                commandModel.setTimeComplexity(cellValue);
                break;

            default:break;
        }

        this.fireTableCellUpdated(rowIndex, columnIndex);
    }

    @Override
    public void addTableModelListener(TableModelListener l) {

    }

    @Override
    public void removeTableModelListener(TableModelListener l) {

    }

    public void refreshModel(String keyword, String group) {
        this.initData(keyword, group);
//        System.out.println("当前展示命令：" + JSON.toJSONString(this.dataList));

        this.fireTableDataChanged();
    }
}
